import Vue from 'vue'
import VueRouter from 'vue-router'
import { config } from '@/config/global.config'
import ShopLogin from '../components/ShopLogin.vue'
import ShopHome from '../components/ShopHome.vue'
import ShopWelcome from '@/components/ShopWelcome'
import ShopUsers from '@/components/users/ShopUsers'
import ShopParams from '@/components/goods/ShopParams'
import ShopCategories from '@/components/goods/ShopCategories'
import ShopGoods from '@/components/goods/ShopGoods'
import ShopOrders from '@/components/orders/ShopOrders'
import ShopReports from '@/components/reports/ShopReports'
import ShopRights from '@/components/rights/ShopRights'
import ShopRoles from '@/components/rights/ShopRoles'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: ShopLogin
  },
  {
    path: '/home',
    component: ShopHome,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: ShopWelcome
      }, {
        path: '/users',
        component: ShopUsers
      }, {
        path: '/roles',
        component: ShopRoles
      }, {
        path: '/rights',
        component: ShopRights
      }, {
        path: '/goods',
        component: ShopGoods
      }, {
        path: '/params',
        component: ShopParams
      }, {
        path: '/categories',
        component: ShopCategories
      }, {
        path: '/orders',
        component: ShopOrders
      }, {
        path: '/reports',
        component: ShopReports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !config.authorization.token) next('/login')
  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})

export default router
